﻿#region

using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;

#endregion

namespace Radovici.SharedLibrary
{
    public class BooleanToVisibilityConverter : IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            try
            {
                //if (string.Equals(parameter as string, "Debug")) Debug.WriteLine(value.ToString());
                if (value == null || !typeof (bool).IsAssignableFrom(value.GetType()))
                {
                    value = (value != null);
                }
                bool val = System.Convert.ToBoolean(value);
                if (parameter != null && parameter.ToString().Equals("not"))
                {
                    val = !val;
                }
                return val
                           ? Visibility.Visible
                           : Visibility.Collapsed;
            }
            catch
            {
                return Visibility.Visible;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            try
            {
                bool val = (Visibility) value == Visibility.Visible;
                if (parameter != null && parameter.ToString().Equals("not"))
                {
                    val = !val;
                }
                return val;
            }
            catch
            {
                return false;
            }
        }

        #endregion
    }

    public class IsEqualToConverter : IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value == parameter;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException("IsStringEqualToConverter's ConvertBack not implemented.");
        }

        #endregion
    }
}